<?php

namespace app\common\util;

use app\admin\model\GlobalConfig;
use OSS\Core\OssException;
use OSS\OssClient;

class Oss
{
    /**
     *阿里云
     */
    public static function alYunOSS($filePath, $Extension, $path, $name = '')
    {
        global $globalConfig;
        $accessKeyId = $globalConfig['file_accessKeyId'];
        $accessKeySecret = $globalConfig['file_accessKeySecret'];
        $endpoint = $globalConfig['file_endpoint'];
        $bucket = $globalConfig['file_OssName'];
        $picUrl = $globalConfig['file_pic_url'] ?: '';
        if ($name) {
            $object = $path . '/' . date("Ymd") . '/' . $name . '.' . $Extension;    // 文件名称
        } else {
            $object = $path . '/' . date("Ymd") . '/' . time() . rand(10000, 99999) . '.' . $Extension;    // 文件名称
        }
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $option = [];
            if ($Extension == 'jpg' || $Extension == 'jpeg' || $Extension == 'png') {
                $TYPE = 'image/jpg';
                $option = [OssClient::OSS_CONTENT_TYPE => $TYPE];
            } else if ($Extension == 'pptx') {
                $TYPE = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
                $option = [OssClient::OSS_CONTENT_TYPE => $TYPE];
            }
            $ossClient->uploadFile($bucket, $object, $filePath, $option);
            if (!is_object($filePath)) delUploadFile($filePath);
            return ['code' => 200, 'src' => $picUrl . $object];
        } catch (OssException $e) {
            return ['code' => 201, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 分片上传
     * @param $path
     * @param $name
     * @return array
     */
    public static function fileToOssFragmentation($path, $name = '')
    {
        global $globalConfig;
        $ext = strrchr($path, '.');
        $ext = $ext == '.jpeg' ? '.jpg' : $ext;
        $filename = $name ?: uniqid() . $ext;
        $endpoint = $globalConfig['file_endpoint'] ?: '';
        $picUrl = $globalConfig['file_pic_url'] ?: '';
        $bucket = $globalConfig['file_OssName'];
        $object = $path . '/' . date("Ymd") . '/' . $filename;    // 文件名称
        /**
         * 手动设置header
         * 不然图片链接表现为下载而不是预览
         */
        $options = array(
            OssClient::OSS_CHECK_MD5 => true,
            OssClient::OSS_PART_SIZE => 1,
        );
        if (strstr($path, '.jpg') !== false || strstr($path, '.jpeg') !== false || strstr($path, '.png') !== false) {
            $TYPE = 'image/jpg';
            $options[OssClient::OSS_CONTENT_TYPE] = $TYPE;
        } else if (strstr($path, '.pptx') !== false) {
            $TYPE = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
            $options[OssClient::OSS_CONTENT_TYPE] = $TYPE;
        }

        try {
            $config = array(
                "provider" => new RewriteOssProvider(),
                "endpoint" => $endpoint,
                "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
                "region" => "cn-chengdu"
            );
            $ossClient = new OssClient($config);

            $ossClient->multiuploadFile($bucket, $object, $path, $options);
            delUploadFile($path);
            return ['code' => 200, 'src' => $picUrl . $object];
        } catch (OssException $e) {
            return ['code' => 201, 'msg' => $e->getMessage()];
        }
    }

    /**
     *删除oss
     */
    public static function alYunDel($path)
    {
        $data = GlobalConfig::getValues(['file_accessKeyId', 'file_accessKeySecret', 'file_endpoint', 'file_OssName']);
        $accessKeyId = $data['file_accessKeyId'];
        $accessKeySecret = $data['file_accessKeySecret'];
        $endpoint = $data['file_endpoint'];
        $bucket = $data['file_OssName'];
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, true);
            $ossClient->deleteObject($bucket, $path);
            return true;
        } catch (OssException $e) {
            return $e->getMessage();
        }
    }
}